﻿//-----------------------------------------------------------------------
// <copyright file="Log4NetLog.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: Log4NetLog.cs
// * history : created by qinchaoyue 2017-11-21 05:21:06
// </copyright>
//-----------------------------------------------------------------------

using log4net;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Slwy.Logger.Models;

namespace Slwy.Logger.Implements.Log4Net
{
    using System;

    using Slwy.Utility.ConfigurManager;
    using Slwy.Utility.DebugLog;
    using Slwy.Utility.Extensions;

    /// <inheritdoc />
    /// <summary>
    /// The log 4 net log.
    /// </summary>
    public class Log4NetLog : LogBase
    {
        /// <summary>
        /// acc 日志记录器
        /// </summary>
        // private readonly ILog _accDetailLog = CustomRollingFileLogger.GetCustomLogger("AccDetail");
        private readonly ILog _accDetailLog;

        /// <summary>
        /// Exception 日志记录器
        /// </summary>
        // private readonly ILog _exceptionDetailLog = CustomRollingFileLogger.GetCustomLogger("ExceptionDetail");
        private readonly ILog _exceptionDetailLog;

        /// <summary>
        /// InteractiveDetail 日志记录器
        /// </summary>
        //private readonly ILog _interactiveDetailLog = CustomRollingFileLogger.GetCustomLogger("InteractiveDetail");
        private readonly ILog _interactiveDetailLog;

        private readonly IOptions<LogOptions> _logOptions;

        /// <summary>
        /// The _log level.
        /// </summary>
        private int _logLevel;

        /// <summary>
        /// Initializes a new instance of the <see cref="Log4NetLog"/> class.
        /// </summary>
        public Log4NetLog(CustomRollingFileLogger customRollingFileLogger, IOptions<LogOptions> logOptions)
        {
            this._logOptions = logOptions;
            this._accDetailLog = customRollingFileLogger.GetCustomLogger("AccDetail");
            this._exceptionDetailLog = customRollingFileLogger.GetCustomLogger("ExceptionDetail");
            this._interactiveDetailLog = customRollingFileLogger.GetCustomLogger("InteractiveDetail");
            this.SetLogLevel();
        }

        private void SetLogLevel()
        {
            var logLevel = this._logOptions.Value.LogLevel;
            if (logLevel.IsNullOrEmpty())
            {
                logLevel = "error";
            }
            switch (logLevel.ToUpper())
            {
                case "DEBUG":
                    this._logLevel = 4;
                    break;
                case "INFO":
                    this._logLevel = 3;
                    break;
                case "WARN":
                    this._logLevel = 2;
                    break;
                case "ERROR":
                    this._logLevel = 1;
                    break;
                default:
                    this._logLevel = 4;
                    break;
            }
        }

        /// <summary>
        /// The write.
        /// </summary>
        /// <param name="level">
        /// The level.
        /// </param>
        /// <param name="logType">
        /// The log Type.
        /// </param>
        /// <param name="message">
        /// The log entry.
        /// </param>
        protected override void Write(LogLevel level, LogType logType, string message)
        {
            try
            {
                if (logType == LogType.Interactive)
                {
                    this._interactiveDetailLog.Info(message);
                    return;
                }

                if (logType == LogType.Acc)
                {
                    this._accDetailLog.Info(message);
                    return;
                }

                switch (level)
                {
                    case LogLevel.Debug:
                        if (this._logLevel >= 4)
                        {
                            this._exceptionDetailLog.Info(message);
                        }

                        break;
                    case LogLevel.Information:
                        if (this._logLevel >= 3)
                        {
                            this._exceptionDetailLog.Info(message);
                        }

                        break;
                    case LogLevel.Warning:
                        if (this._logLevel >= 2)
                        {
                            this._exceptionDetailLog.Info(message);
                        }

                        break;
                    case LogLevel.Error:
                        if (this._logLevel >= 1)
                        {
                            this._exceptionDetailLog.Info(message);
                        }

                        break;
                    default:
                        if (this._logLevel >= 4)
                        {
                            this._exceptionDetailLog.Info(message);
                        }

                        break;
                }
            }
            catch (Exception e)
            {
                Logs.Err("方法 Slwy.Logger.Implements.Log4Net.Log4NetLog.Write 错误: " + e);
            }
        }
    }
}
